package com.sdry.web.controller.zc.quality;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.llm.Department;
import com.sdry.model.llm.User;
import com.sdry.model.workplace.Centrifugal;
import com.sdry.model.workplace.CentrifugalExample;
import com.sdry.model.workplace.CentrifugalExample.Criteria;
import com.sdry.model.zc.ZcDisposeRejectsDetailsEntity;
import com.sdry.model.zc.ZcDisposeRejectsEntity;
import com.sdry.model.zc.ZcEvaluateEntity;
import com.sdry.model.zc.ZcGasEntity;
import com.sdry.model.zc.ZcLeakHuntingEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.model.zc.ZcTestDetailsEntity;
import com.sdry.model.zc.ZcTestEntity;
import com.sdry.service.jyy.CutServise;
import com.sdry.service.jyy.SolidServise;
import com.sdry.service.workplace.CentrifugalServise;
import com.sdry.service.zc.ZcDisposeRejectsService;
import com.sdry.utils.PushExample;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcDisposeRejectsController   
 * @Description:  不合格处置（生产过程中的疑似不良品处置模块，处理报检的产品）
 * @Author:       zc   
 * @CreateDate:   2018年9月25日 下午5:17:10   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("disposeRejects")
public class ZcDisposeRejectsController {
	@Resource
    private CentrifugalServise centrifugalServise;
	/**注入固化工位业务层*/
	@Resource
	private SolidServise solidServise;
	/**注入切割工位业务层*/
	@Resource
	private CutServise cutServise;
	@Resource
	private ZcDisposeRejectsService zdService;
	/**
	 * 进入不合格处置页面
	 * @return
	 */
	@RequestMapping("enterDisposeRejectsPage")
	public String enterDisposeRejectsPage(){
		return "/zc/quality/disposeRejects";
	}
	/**
	 * 查询不合格处置列表
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectDisposeRejectsList")
	public void selectDisposeRejectsList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		List<ZcDisposeRejectsEntity> selectDisposeRejectsList = zdService.selectDisposeRejectsList(zcQueryCriteria);
		int count = zdService.countDisposeRejectsList();
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectDisposeRejectsList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 新增处置单
	 * @param request
	 * @param response
	 * @param zcDisposeRejectsEntity
	 */
	@RequestMapping("insertDisposeRejects")
	public void insertDisposeRejects(HttpServletRequest request, HttpServletResponse response,ZcDisposeRejectsEntity zcDisposeRejectsEntity,HttpSession httpSession){
		Date date = new Date();
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcDisposeRejectsEntity.setdDisponer(userName);
		zcDisposeRejectsEntity.setdDisposeDate(date);
		int insertDisposeRejects = zdService.insertDisposeRejects(zcDisposeRejectsEntity);
		try {
			ResponseUtil.write(response, insertDisposeRejects);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 修改处置单
	 * @param request
	 * @param response
	 * @param zcDisposeRejectsEntity
	 */
	@RequestMapping("updateDisposeRejects")
	public void updateDisposeRejects(HttpServletRequest request, HttpServletResponse response,ZcDisposeRejectsEntity zcDisposeRejectsEntity,HttpSession httpSession){
		int updateDisposeRejects = zdService.updateDisposeRejects(zcDisposeRejectsEntity);
		try {
			ResponseUtil.write(response, updateDisposeRejects);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @param idArr
	 */
	@RequestMapping("delDisposeRejectsArr")
	public void delDisposeRejectsArr(HttpServletRequest request, HttpServletResponse response,@RequestParam("idArr[]") String[] idArr){
		int affact = 0;
		for (String did : idArr) {
			long id = Long.parseLong(did);
			affact += zdService.delDisposeRejectsArr(id);
		}
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 发起审批
	 * @param request
	 * @param response
	 * @param zcDisposeRejectsEntity
	 */
	@RequestMapping("sponsorApprover")
	public void sponsorApprover(HttpServletRequest request, HttpServletResponse response,ZcDisposeRejectsEntity zcDisposeRejectsEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcDisposeRejectsEntity.setRemark04(userName);
		int affact = zdService.sponsorApprover(zcDisposeRejectsEntity);
		String s = zcDisposeRejectsEntity.getdApprover1();
		int testSendPush = PushExample.testSendPush("您有一个新的不合格处置单需要审批", "您有一个新的不合格处置单需要审批", s, "1");
		System.err.println(testSendPush);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过处置单id查找详情
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectDisposeRejectsDetailsListBydId")
	public void selectDisposeRejectsDetailsListBydId(HttpServletRequest request, HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcDisposeRejectsDetailsEntity> selectDisposeRejectsDetailsListBydId = zdService.selectDisposeRejectsDetailsListBydId(zcQueryCriteria);
		int count = zdService.countDisposeRejectsDetailsListBydId(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectDisposeRejectsDetailsListBydId, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 安卓通过处置单id查找详情
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectDisposeRejectsDetailsListBydIdAndroid")
	public void selectDisposeRejectsDetailsListBydIdAndroid(HttpServletRequest request,HttpServletResponse response,String id){
		
		List<ZcDisposeRejectsDetailsEntity> disposeRejectsList = zdService.selectDisposeRejectsDetailsListBydIdAndroid(id);
		
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(disposeRejectsList);
		try {
			if(disposeRejectsList.size()>0){
				jsonObject.put("state", 1);
				jsonObject.put("message", "查找成功");
				jsonObject.put("data", jsonArray);
			}else{
				jsonObject.put("state", 0);
				jsonObject.put("message", "查找失败");
				jsonObject.put("data", null);
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查找质检单列表
	 * @return
	 */
	@RequestMapping("selectTestList")
	public void selectTestList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcTestEntity> testList = zdService.selectTestList(zcQueryCriteria);
		int count = zdService.countTestList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(testList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查找质检单
	 * @param zqc
	 * @param request
	 * @param response
	 */
	@RequestMapping("selectTestByNumber")
	public void selectTestByNumber(ZcQueryCriteria zqc,HttpServletRequest request, HttpServletResponse response){
		String keyword = zqc.getKeyword();
		if(keyword == null){
			keyword = "";
		}
		ZcTestEntity zcTestEntity = zdService.selectTest(zqc);
		JSONObject json= JSONObject.fromObject(zcTestEntity);  
		try {
			if(zcTestEntity!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存处置单详情
	 * @param request
	 * @param response
	 * @param zcDisposeRejectsDetailsEntity
	 */
	@RequestMapping("insertDisposeRejectsDetails")
	public void insertDisposeRejectsDetails(HttpServletRequest request, HttpServletResponse response,ZcDisposeRejectsDetailsEntity zcDisposeRejectsDetailsEntity){
		int affact = zdService.insertDisposeRejectsDetails(zcDisposeRejectsDetailsEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 修改处置单详情
	 * @param request
	 * @param response
	 * @param zcDisposeRejectsDetailsEntity
	 */
	@RequestMapping("updateDisposeRejectsDetails")
	public void updateDisposeRejectsDetails(HttpServletRequest request, HttpServletResponse response,ZcDisposeRejectsDetailsEntity zcDisposeRejectsDetailsEntity){
		int affact = zdService.updateDisposeRejectsDetails(zcDisposeRejectsDetailsEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除子表详情信息
	 * @param request
	 * @param response
	 * @param idArr
	 */
	@RequestMapping("delDisposeRejectsArrDetails")
	public void delDisposeRejectsArrDetails(HttpServletRequest request, HttpServletResponse response,@RequestParam("idArr[]") String[] idArr){
		int affact = 0;
		for (String ddId : idArr) {
			long id = Long.parseLong(ddId);
			affact += zdService.delDisposeRejectsArrDetails(id);
		}
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 树结构
	 * @param request
	 * @param response
	 */
	@RequestMapping("getZtreeJson")
	public void getZtreeJson(HttpServletRequest request, HttpServletResponse response){
		//部门树
		List<Department> getZtreeJsonList = zdService.getZtreeJson();
        JSONArray jsonArray =  new JSONArray();
        for (Department d : getZtreeJsonList) {
        	JSONObject json=new JSONObject();
			json.put("id", d.getDid());
			json.put("pId", "-1");
			json.put("type", "-1");
			json.put("name", d.getName());
			json.put("open", false);
			json.put("isParent", true);
			json.put("nocheck",true);
			jsonArray.add(json);
			//员工树
			List<User> getZtreeChild = zdService.getZtreeChild(d.getDid());
			for (User user : getZtreeChild) {
				 JSONObject jsonemp=new JSONObject();
				// jsonemp.put("cid", staff.getStaid());
				 jsonemp.put("pId", d.getDid());
				 jsonemp.put("type", "0");//部门人员
				 jsonemp.put("name", user.getUserName());
				 jsonemp.put("open", false);
				 jsonArray.add(jsonemp);
			}
		}
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", jsonArray);
        try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询上次审批人
	 * @param request
	 * @param response
	 */
	@RequestMapping("selectHistoryApprover")
	public void selectHistoryApprover(HttpServletRequest request, HttpServletResponse response){
		ZcDisposeRejectsEntity zcDisposeRejectsEntity = zdService.selectHistoryApprover();
		JSONObject json= JSONObject.fromObject(zcDisposeRejectsEntity);  
		try {
			if(zcDisposeRejectsEntity!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过质检单号查询检漏结果
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectLeakHuntingListByNumber")
	public void selectLeakHuntingListByNumber(HttpServletRequest request, HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcLeakHuntingEntity> selectLeakHuntingListByNumber = zdService.selectLeakHuntingListByNumber(zcQueryCriteria);
		int count = zdService.countLeakHuntingListByNumber(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectLeakHuntingListByNumber, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过质检单号查询组件评价结果
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectEvaluateListByNumber")
	public void selectEvaluateListByNumber(HttpServletRequest request, HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcEvaluateEntity> selectEvaluateListByNumber = zdService.selectEvaluateListByNumber(zcQueryCriteria);
		int count = zdService.countEvaluateListByNumber(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectEvaluateListByNumber, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过质检单号查询气密性检测结果
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectGasListByNumber")
	public void selectGasListByNumber(HttpServletRequest request, HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcGasEntity> selectGasListByNumber = zdService.selectGasListByNumber(zcQueryCriteria);
		int count = zdService.countGasListByNumber(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectGasListByNumber, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过质检单号查询配件检测结果
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectTestListByNumber")
	public void selectTestListByNumber(HttpServletRequest request, HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<ZcTestDetailsEntity> selectTestListByNumber = zdService.selectTestListByNumber(zcQueryCriteria);
		int count = zdService.countTestListByNumber(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectTestListByNumber, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过did查询不合格处置单
	 * @param request
	 * @param response
	 * @param dId
	 */
	@RequestMapping("selectDisposeRejectsBydId")
	public void selectDisposeRejectsBydId(HttpServletRequest request, HttpServletResponse response,long dId){
		List<ZcDisposeRejectsEntity> zcDisposeRejectsEntity = zdService.selectDisposeRejectsBydId(dId);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(zcDisposeRejectsEntity, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 组件报废处理
	 * @param code
	 */
	@RequestMapping("baofei")
	public void baofei(ZcDisposeRejectsEntity zcDisposeRejectsEntity){
		edit(zcDisposeRejectsEntity.getCode());
		baoFeiCentrifugalCtube(zcDisposeRejectsEntity.getCode());
		updateNumbered(zcDisposeRejectsEntity);
	}
	/**
	 * 修改各工位膜管为报废状态
	 * @param code 膜管条码
	 */
	public void edit(String code) {
		//固化工位
		solidServise.editcremark6(code);
		//切割工位
		cutServise.editcremark6(code);
	}
	/**
     * 根据膜管编号报废离心机工位膜管
     * @param ctube 膜管编号
     */
	public void baoFeiCentrifugalCtube(String ctube){
		CentrifugalExample example = new CentrifugalExample();
		Criteria createCriteria = example.createCriteria();
		// 膜管编码
		createCriteria.andCtubeEqualTo(ctube);
		// 是否报废 1是 0否
		createCriteria.andCremark1EqualTo("0");
		List<Centrifugal> centrifugals = centrifugalServise
				.selectByExample(example);
		if (centrifugals.size() == 1) {
			Centrifugal centrifugal = centrifugals.get(0);
			// 是否报废 1是 0否
			centrifugal.setCremark1("1");
			// 修改
			centrifugalServise.updateByPrimaryKeySelective(centrifugal );
		}
	}
	/**
	 * 报废改变数点表
	 * @param code
	 */
	public int updateNumbered(ZcDisposeRejectsEntity zcDisposeRejectsEntity){
		return zdService.updateNumbered(zcDisposeRejectsEntity);
	}
}
